//
//  WBHomeViewController.swift
//  仿写微博
//
//  Created by Apple on 17/6/30.
//  Copyright © 2017年 tuimao. All rights reserved.
//

import UIKit

//原创微博cellID
private let originalCellID = "originalCellID"
private let retweetedlCellcellID = "retweetedlCellcellID"
class WBHomeViewController: WBBaseViewController {
    //数据数组
    fileprivate lazy var listViewModel = WBStatusListViewModel()
    //加载数据
    override func loadData() {
        // Xcode 8.0 的刷新控件，beginRefreshing 方法，什么都不显示！
        refreshControl.beginRefreshing()        
        //发送微博home数据
        listViewModel.loadStatus(pullup:self.isPullup){ (issuccess,shouldRefrsh) in
            self.isPullup = false
            //结束刷新
            self.refreshControl.endRefreshing()
            //当没到最大的刷新数目的时候，我们再刷新表格
            if shouldRefrsh {
                //刷新表格
                self.tableView?.reloadData()
            }
        }
}
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    //letBaritem的点击
    @objc fileprivate func showFriend(){
        let demo = WBDemoViewController()
        navigationController?.pushViewController(demo, animated: true)
    }
}

//MARK: 设置界面
extension WBHomeViewController{
    //重写父类方法
    override func setupTableView() {
        super.setupTableView()
        //设置左BarButtonItem
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriend),isback:false)
        //注册表
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: originalCellID)
        tableView?.register(UINib(nibName: "WBStatusRetweetedlCell", bundle: nil), forCellReuseIdentifier: retweetedlCellcellID)
        //FIXME:预估行高和自适应
        tableView?.estimatedRowHeight = 300
        tableView?.rowHeight = UITableViewAutomaticDimension
        //取消分割线
        tableView?.separatorStyle = .none
        //设置titleView
        setupNavTitle()
    }
    
    //MARK: 设置titleView
    private func setupNavTitle(){
        //标题
        let title = WBNetworkManager.shared.useAccount.screen_name
        //这只按钮的标题
        let button = WBTitleButton(title: title)
        //添加到titleView
        navItem.titleView = button
        //button的点击事件
        button.addTarget(self, action: #selector(clickTitleBtn), for: .touchUpInside)
    }
    //titleBtn的点击事件
    @objc private func clickTitleBtn(btn:UIButton){
        btn.isSelected = !btn.isSelected
    }
}

//MARK: tableView的Datasource
extension WBHomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let vm = listViewModel.statusList[indexPath.row]
        
        let cellID = (vm.status.retweeted_status != nil) ? retweetedlCellcellID : originalCellID
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! WBStatusCell
        
        //赋值cell的模型
        cell.viewModel = vm
        //实现cell代理
        cell.delegate = self
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let vm = listViewModel.statusList[indexPath.row]
        
        return vm.rowHeight
    }
}
//MARK:  实现微博cell的代理
extension WBHomeViewController: WBStatusCellDelegate{
    func statusCellDidTapURLString(cell: WBStatusCell, urlString: String) {
        print(urlString)
        let vc = WBWebViewController()
        vc.urlString = urlString
        navigationController?.pushViewController(vc, animated: true)
    }
}




